<#assign contextPath=rc.contextPath/> <#include
"/common/__context.html"/> <#assign pageTitle="VIGOR"/> <#assign
pageBreadCrumbs>
<li class="active">会话列表</li>
</#assign> <#assign currentMenu="menu1-menu5"/>
<!-- page specific plugin scripts -->
<script src="${contextPath}/static/js/jquery.dataTables.js"></script>
<script src="${contextPath}/static/js/jquery.dataTables.bootstrap.js"></script>
<script src="${contextPath}/static/js/fnReloadAjax.js"></script>
<!-- inline scripts related to this page -->
<script type="text/javascript">
	var oTable1;
	jQuery(function($) {
		oTable1 = $('#session-table')
				.dataTable(
						{
							"bFilter" : false,
							"bAutoWidth" : true,
							"bServerSide" : true,
							"aaSorting" : [],
							"sAjaxSource" : "${contextPath}/session/list",
							"fnServerData" : retrieveData,
							"aoColumns" : [ {
								"bVisible" : true,
								"bSortable" : false,
								"sClass" : "center",
								"mData" : "",
								"aTargets" : [ 0 ]
							}, {
								"bVisible" : true,
								"mData" : "id",
								"aTargets" : [ 1 ]
							}, {
								"bVisible" : true,
								"mData" : "user",
								"aTargets" : [ 2 ]
							}, {
								"bVisible" : true,
								"mData" : "host",
								"aTargets" : [ 3 ]
							}, {
								"bVisible" : true,
								"mData" : "last",
								"aTargets" : [ 4 ]
							}, {
								"bVisible" : true,
								"mData" : "force",
								"aTargets" : [ 5 ]
							}, ],
							"aoColumnDefs" : [ {
								sDefaultContent : '',
								aTargets : [ '_all' ]
							} ],
							"fnRowCallback" : function(nRow, aData, iDataIndex) {
								$('td:eq(0)', nRow)
										.html(
												'<label class="position-relative"><input type="checkbox" class="ace" value="'+aData['id']+'"/> <span class="lbl"></span></label>');
								$('td:eq(1)', nRow).html(aData['id']);
								$('td:eq(2)', nRow).html(aData['user']);
								$('td:eq(3)', nRow).html(aData['host']);
								$('td:eq(4)', nRow).html(aData['last']);
								$('td:eq(5)', nRow).html(
										aData['force'] ? '是' : '否');
							},
							"oLanguage" : {
								"sProcessing" : "正在加载中......",
								"sLengthMenu" : "每页显示 _MENU_ 条记录",
								"sZeroRecords" : "对不起,查询不到相关数据!",
								"sEmptyTable" : "无记录存在!",
								"sInfo" : "当前显示 _START_ 到 _END_ 条,共 _TOTAL_ 条记录",
								"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
								"sSearch" : "搜索",
								"oPaginate" : {
									"sFirst" : "首页",
									"sPrevious" : "上一页",
									"sNext" : "下一页",
									"sLast" : "末页"
								}
							}
						});

		function retrieveData(sSource, aoData, fnCallback) {
			$.ajax({
				"type" : "POST",
				"contentType" : "application/json",
				"url" : sSource,
				"dataType" : "json",
				"data" : JSON.stringify(aoData), //以json格式传递   
				"success" : function(result) {
					fnCallback(result); //服务器端返回的对象的returnObject部分是要求的格式
				}
			});
		}

		$(document).on(
				'click',
				'th input:checkbox',
				function() {
					var that = this;
					$(this).closest('table').find(
							'tr > td:first-child input:checkbox').each(
							function() {
								this.checked = that.checked;
								$(this).closest('tr').toggleClass('selected');
							});
				});

	})

	function forceLogout(id) {
		ajaxUtil("${contextPath}/session/" + id + "/forceLogout", id);
	}

	function forceLogoutSelect() {
		var ids = "";
		var checkboxs = $("input[type='checkbox']");
		checkboxs.each(function(i) {
			if ($(this).is(":checked")) {
				ids += $(this).val() + ",";
			}
		})
		if (ids.indexOf("0,") != -1) {
			ids = ids.substring(2, ids.length - 1);
		} else {
			ids = ids.substring(0, ids.length - 1);
		}

		ajaxUtil("${contextPath}/session/forceLogoutSelect", "ids=" + ids);
	}

	function ajaxUtil(url, data) {
		$
				.ajax({
					"type" : "get",
					"contentType" : "application/json",
					"url" : url,
					"dataType" : "json",
					"data" : data,
					"success" : function(result) {
						if (result.success) {
							$('#messageBox')
									.html(
											'<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button><strong> <i class="ace-icon fa fa-check"></i> </strong> '
													+ result.message
													+ ' <br /></div>').hide()
									.fadeTo(1000, 1);
							// 重新加载datatables
							oTable1.fnReloadAjax();
						} else {
							$('#messageBox')
									.html(
											'<div class="alert alert-danger"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button><strong> <i class="ace-icon fa fa-times"></i> </strong> '
													+ result.message
													+ ' <br /></div>').hide()
									.fadeTo(1000, 1);
						}
					}
				});
	}
</script>

<@mainContext pageTitle=pageTitle pageBreadCrumbs=pageBreadCrumbs
currentMenu=currentMenu>
<div class="page-header">
	<button type="button" class="btn btn-white btn-sm btn-success"
		onclick="forceLogoutSelect();">
		<i class="ace-icon glyphicon glyphicon-off smaller-80"></i>强制退出所选用户
	</button>
</div>
<div id="messageBox"></div>
<!-- pageContent BEGAIN -->
<div class="row">
	<div class="col-xs-12">
		<!-- PAGE CONTENT BEGINS -->
		<div class="row">
			<div class="col-xs-12">
				<div class="table-header">会话列表</div>

				<div>
					<table id="session-table"
						class="table table-striped table-bordered table-hover">
						<thead>
							<tr>
								<th class="center"><label class="position-relative">
										<input id="allSelect" type="checkbox" class="ace" value="0" />
										<span class="lbl"></span>
								</label></th>
								<th>会话编号</th>
								<th>用户名</th>
								<th>主机</th>
								<th>最后活动时间</th>
								<th>是否强制退出</th>
							</tr>
						</thead>

						<tbody>

						</tbody>
					</table>
				</div>
			</div>
		</div>
		<!-- PAGE CONTENT ENDS -->
	</div>
	<!-- /.col -->
</div>
<!-- pageContent END -->
</@mainContext>
